package com.qin.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qin.db.DBAccess;
import com.qin.entity.VerSysEnv;
import com.qin.enums.RetCode;
import com.qin.mapper.VerSysEnvMapper;
import com.qin.service.RelationService;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RelationServiceImpl implements RelationService {
    @Override
    public JSONObject queryVerSysEnvs(VerSysEnv verSysEnv) {

//        System.out.println("service层：queryVerSysEnvs");

        //调用Dao层
        SqlSession sqlSession = null;
        Map map = new HashMap();
        JSONArray retArray;

        try {
            sqlSession = DBAccess.getSqlSession();
            VerSysEnvMapper verSysEnvMapper = sqlSession.getMapper(VerSysEnvMapper.class);

            List retList = verSysEnvMapper.queryVerSysEnvs(verSysEnv);
            retArray = new JSONArray(retList);

        } catch (Exception e) {
            e.printStackTrace();
            Map errMap = new HashMap();
            errMap.put("retCode", RetCode.ERROR.getCode());
            errMap.put("retMsg", RetCode.ERROR.getMsg());
            errMap.put("errorMsg", e.getMessage());
            JSONObject retJson = new JSONObject(errMap);

            return retJson;
        }  finally {
            if(sqlSession != null){
                sqlSession.close();
            }
        }

        //拼接返回JSON
        map.put("verSysEnvInfo", retArray);
        map.put("retCode", RetCode.SUCCESS.getCode());
        map.put("retMsg", RetCode.SUCCESS.getMsg());
        JSONObject retJson = new JSONObject(map);

        return retJson;
    }

    @Override
    public JSONObject insertVerSysEnv(VerSysEnv verSysEnv) {

//        System.out.println("service层：insertVerSysEnv");

        //调用Dao层
        SqlSession sqlSession = null;
        Map map = new HashMap();
        boolean isSuccess;

        try {
            sqlSession = DBAccess.getSqlSession();
            VerSysEnvMapper verSysEnvMapper = sqlSession.getMapper(VerSysEnvMapper.class);

            isSuccess = verSysEnvMapper.insertVerSysEnv(verSysEnv);

        } catch (Exception e) {
            e.printStackTrace();
            Map errMap = new HashMap();
            errMap.put("retCode", RetCode.ERROR.getCode());
            errMap.put("retMsg", RetCode.ERROR.getMsg());
            errMap.put("errorMsg", e.getMessage());
            JSONObject retJson = new JSONObject(errMap);

            return retJson;
        } finally {
            if(sqlSession != null){
                sqlSession.commit();
                sqlSession.close();
            }
        }

        //拼接返回JSON
        if(isSuccess == true){
            map.put("retCode", RetCode.SUCCESS.getCode());
            map.put("retMsg", RetCode.SUCCESS.getMsg());
        }else{
            map.put("retCode", RetCode.ERROR.getCode());
            map.put("retMsg", RetCode.ERROR.getMsg());
            map.put("errorMsg", "执行sql出错或没有记录被插入");
        }
        JSONObject retJson = new JSONObject(map);

        return retJson;
    }

    @Override
    public JSONObject deleteVerSysEnv(VerSysEnv verSysEnv) {

//        System.out.println("service层：deleteVerSysEnv");

        //调用Dao层
        SqlSession sqlSession = null;
        Map map = new HashMap();
        boolean isSuccess;

        try {
            sqlSession = DBAccess.getSqlSession();
            VerSysEnvMapper verSysEnvMapper = sqlSession.getMapper(VerSysEnvMapper.class);

            isSuccess = verSysEnvMapper.deleteVerSysEnv(verSysEnv);

        } catch (Exception e) {
            e.printStackTrace();
            Map errMap = new HashMap();
            errMap.put("retCode", RetCode.ERROR.getCode());
            errMap.put("retMsg", RetCode.ERROR.getMsg());
            errMap.put("errorMsg", e.getMessage());
            JSONObject retJson = new JSONObject(errMap);

            return retJson;
        } finally {
            if(sqlSession != null){
                sqlSession.commit();
                sqlSession.close();
            }
        }

        //拼接返回JSON
        if(isSuccess == true){
            map.put("retCode", RetCode.SUCCESS.getCode());
            map.put("retMsg", RetCode.SUCCESS.getMsg());
        }else{
            map.put("retCode", RetCode.ERROR.getCode());
            map.put("retMsg", RetCode.ERROR.getMsg());
            map.put("errorMsg", "执行sql出错或没有记录被删除");
        }
        JSONObject retJson = new JSONObject(map);

        return retJson;
    }
}
